package org.jeecg.modules.wangyue.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.ActionTypeConstant;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.ShowStateConstant;
import org.jeecg.modules.wangyue.entity.Banner;
import org.jeecg.modules.wangyue.model.req.ReqCommonModel;
import org.jeecg.modules.wangyue.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
* @Description: 轮播图管理
* @Author: jeecg-boot
* @Date:   2023-07-24
* @Version: V1.0
*/
@Slf4j
@Api(tags="轮播图管理")
@RestController
@RequestMapping("/wangyue/sys/banner")
public class BannerManageController extends JeecgController<Banner, IBannerService> {
   @Autowired
   private IBannerService bannerService;


    /**
     * 轮播图管理-分页列表查询
     *
     * @param pageNo    第几页
     * @param pageSize  每页大小
     * @return
     */
    @AutoLog(value = "轮播图管理-分页列表查询")
    @ApiOperation(value="轮播图管理-分页列表查询", notes="轮播图管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlagConstant.normal);
        queryWrapper.orderByAsc("sort").orderByDesc("create_time");
        Page<Banner> page = new Page<Banner>(pageNo, pageSize);
        IPage<Banner> pageList = bannerService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 轮播图管理-显示/隐藏
     *
     * @param banner
     * @return
     */
    @AutoLog(value = "轮播图管理-显示/隐藏")
    @ApiOperation(value="轮播图管理-显示/隐藏", notes="轮播图管理-显示/隐藏")
    @PostMapping(value = "/showEdit")
    public Result<?> showEdit(@RequestBody Banner banner) {
        String id = banner.getId();
        String showState = banner.getShowState();
        if(oConvertUtils.isEmpty(id) || oConvertUtils.isEmpty(showState)){
            return Result.error("必传项不能为空");
        }
        UpdateWrapper<Banner> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.eq("del_flag", DelFlagConstant.normal);
        updateWrapper.set("show_state", showState);
        if(bannerService.update(updateWrapper)){
            return Result.OK("操作成功！");
        }
        return Result.error("操作失败！");
    }


    /**
     * 轮播图管理-添加或修改
     *
     * @param banner
     * @return
     */
    @AutoLog(value = "轮播图管理-添加或修改")
    @ApiOperation(value="轮播图管理-添加或修改", notes="轮播图管理-添加或修改")
    @PostMapping(value = "/addOrEdit")
    public Result<?> addOrEdit(@RequestBody Banner banner) {
        if(bannerService.saveOrUpdate(banner)){
            return Result.OK("添加或修改成功！");
        }
        return Result.error("添加或修改失败！");
    }


    /**
     * 轮播图管理-通过id删除
     *
     * @param reqCommonModel
     * @return
     */
    @AutoLog(value = "轮播图管理-通过id删除")
    @ApiOperation(value="轮播图管理-通过id删除", notes="轮播图管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@Valid @RequestBody ReqCommonModel reqCommonModel) {
        if(bannerService.removeById(reqCommonModel.getId())){
            return Result.OK("删除成功!");
        }
        return Result.error("删除失败!");
    }



}
